package JaggSoft;

import JaggSoft.Utilidades.*;
import java.awt.Point;
import javax.swing.JOptionPane;
import javax.swing.JTable;

public class Bodegas extends javax.swing.JInternalFrame {
    private int codigo_bodega;
    private boolean nuevo;
    public Bodegas() {
        initComponents();
        this.setLocation(new Point(70,0));
        Bloquear(true);
        BloquearTexto(true);
        CargarBodegas();
        codigo_bodega=-1;
    }
    private void Bloquear(boolean estado){
        jbtnNuevo.setEnabled(estado);
        jbtnGuardar.setEnabled(!estado);
        jbtnModificar.setEnabled(estado);
        jbtnEliminar.setEnabled(estado);
        jbtnCancelar.setEnabled(!estado);
        jtblBodegas.setEnabled(estado);
    }
    private void BloquearTexto(boolean estado){
        jtfNombreBodega.setEditable(!estado);
        jtfUbicacion.setEditable(!estado);
    }
    private void Limpiar(){
        jtfNombreBodega.setText("");
        jtfUbicacion.setText("");
    }
    private void CargarBodegas(){
        String[] columnas={"Id","Nombre","Cantidad","Ubicación"};
        Object[] datos=new Object[3];
        MyModeloTabla modelo=new MyModeloTabla(columnas);
        JTable tabla=Conexion.ObtenerTabla("SELECT id_bodega,nombre,0 AS cantidad,ubicacion FROM bodega",columnas);
        for(int i=0;i<tabla.getRowCount();i++){
            for(int j=0;j<3;j++)
                datos[j]=tabla.getValueAt(i,j);
            modelo.AgregarFila(datos);
        }
        jtblBodegas.setModel(tabla.getModel());
        Util.OcultarColumna(jtblBodegas,0);
        datos=null;
        tabla=null;
    }
    private void SeleccionarDatos(){
        int fila=jtblBodegas.getSelectedRow();
        if(fila!=-1){
            codigo_bodega=(int)jtblBodegas.getValueAt(fila,0);
            jtfNombreBodega.setText((String)jtblBodegas.getValueAt(fila,1));
            jtfUbicacion.setText((String)jtblBodegas.getValueAt(fila,3));
        }
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jtfUbicacion = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtblBodegas = new javax.swing.JTable();
        jtfNombreBodega = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jbtnCancelar = new javax.swing.JButton();
        jbtnEliminar = new javax.swing.JButton();
        jbtnModificar = new javax.swing.JButton();
        jbtnGuardar = new javax.swing.JButton();
        jbtnNuevo = new javax.swing.JButton();

        setClosable(true);
        setResizable(true);
        setTitle("Administración de Bodegas");

        jtblBodegas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtblBodegas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtblBodegasMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jtblBodegas);

        jLabel9.setText("Ubicación:");

        jLabel10.setText("Nombre Bodega:");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Acciones"));

        jbtnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_cancelar.png"))); // NOI18N
        jbtnCancelar.setToolTipText("Cancelar");
        jbtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCancelarActionPerformed(evt);
            }
        });

        jbtnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_eliminar.png"))); // NOI18N
        jbtnEliminar.setToolTipText("Eliminar");
        jbtnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnEliminarActionPerformed(evt);
            }
        });

        jbtnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_modificar.png"))); // NOI18N
        jbtnModificar.setToolTipText("Modificar");
        jbtnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnModificarActionPerformed(evt);
            }
        });

        jbtnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_guardar.png"))); // NOI18N
        jbtnGuardar.setToolTipText("Guardar");
        jbtnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGuardarActionPerformed(evt);
            }
        });

        jbtnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_nuevo.png"))); // NOI18N
        jbtnNuevo.setToolTipText("Nuevo");
        jbtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNuevoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jbtnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addComponent(jbtnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(53, 53, 53)
                .addComponent(jbtnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
                .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(51, 51, 51)
                .addComponent(jbtnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 11, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 647, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(14, 14, 14)
                                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jtfNombreBodega, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                                    .addComponent(jtfUbicacion, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfNombreBodega, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jtfUbicacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jtblBodegasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtblBodegasMouseClicked
        if(jtblBodegas.isEnabled())
            SeleccionarDatos();
    }//GEN-LAST:event_jtblBodegasMouseClicked
    private void jbtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNuevoActionPerformed
        Bloquear(false);
        BloquearTexto(false);
        Limpiar();
        nuevo=true;
    }//GEN-LAST:event_jbtnNuevoActionPerformed
    private void jbtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCancelarActionPerformed
        Limpiar();
        Bloquear(true);
        BloquearTexto(true);
        SeleccionarDatos();
    }//GEN-LAST:event_jbtnCancelarActionPerformed
    private void jbtnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnGuardarActionPerformed
        if(nuevo){
            if(Conexion.EjecutarConsulta("INSERT INTO Bodega(nombre,ubicacion) VALUES('"+jtfNombreBodega.getText().replace("'","\'")+"','"+jtfUbicacion.getText().replace("'","\'")+"');")){
                CargarBodegas();
                JOptionPane.showMessageDialog(null,"¡Bodega Agregada!","Administración de Bodegas",JOptionPane.INFORMATION_MESSAGE);
            }
            else
                JOptionPane.showMessageDialog(null,"No se pudo agregar la bodega.","Administración de Bodegas",JOptionPane.ERROR_MESSAGE);
        }
        else{
            if(jtblBodegas.getSelectedRow()!=-1){
                int id=(int)jtblBodegas.getValueAt(jtblBodegas.getSelectedRow(),0);
                if(Conexion.EjecutarConsulta("UPDATE Bodega SET nombre='"+jtfNombreBodega.getText().replace("'","\'")+"',ubicacion='"+jtfUbicacion.getText().replace("'","\'")+"' WHERE id_bodega="+id)){
                    CargarBodegas();
                    JOptionPane.showMessageDialog(null,"¡Bodega Modificada!","Administración de Bodegas",JOptionPane.INFORMATION_MESSAGE);
                }
                else
                    JOptionPane.showMessageDialog(null,"No se pudo modificar la bodega.","Administración de Bodegas",JOptionPane.ERROR_MESSAGE);
            }
            else
                JOptionPane.showMessageDialog(null,"Debe seleccionar una bodega.","Administración de Bodegas",JOptionPane.WARNING_MESSAGE);
        }
        Bloquear(true);
        BloquearTexto(true);
    }//GEN-LAST:event_jbtnGuardarActionPerformed
    private void jbtnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnModificarActionPerformed
        Bloquear(false);
        BloquearTexto(false);
        nuevo=false;
    }//GEN-LAST:event_jbtnModificarActionPerformed
    private void jbtnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEliminarActionPerformed
        if(jtblBodegas.getSelectedRow()!=-1){
            if(JOptionPane.showConfirmDialog(null,"¿Esta seguro de eliminar la bodega y todos el inventario que hay en ella?","Administración de Bodegas",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
                if(Conexion.EjecutarConsulta("DELETE FROM Bodega Where id_bodega="+jtblBodegas.getValueAt(jtblBodegas.getSelectedRow(),0))){
                    CargarBodegas();
                    JOptionPane.showMessageDialog(null,"¡Bodega Eliminada!","Administración de Bodegas",JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
        else
            JOptionPane.showMessageDialog(null,"Debe seleccionar una bodega.","Administración de Bodegas",JOptionPane.WARNING_MESSAGE);
    }//GEN-LAST:event_jbtnEliminarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JButton jbtnEliminar;
    private javax.swing.JButton jbtnGuardar;
    private javax.swing.JButton jbtnModificar;
    private javax.swing.JButton jbtnNuevo;
    private javax.swing.JTable jtblBodegas;
    private javax.swing.JTextField jtfNombreBodega;
    private javax.swing.JTextField jtfUbicacion;
    // End of variables declaration//GEN-END:variables
}
